package com.shunwang.tools.httpclientsupport.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shunwang.tools.httpclientsupport.exception.HttpExecutorException;
import com.shunwang.tools.httpclientsupport.pool.Pool;

public class HttpClientTemplet {

	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private HttpClientExecutor executor;
	
	protected boolean hasThrows = false;
	
	public HttpClientTemplet(Pool pool){
		this.executor = new HttpClientExecutor(pool);
	}
	
	public HttpClientTemplet(HttpClientExecutor executor){
		this.executor = executor;
	}
	/**
	 * 执行相关查询
	 * @param callback
	 * @return 查询后的内容
	 */
	public Object doExecutor(ClientCallBack callback){
		try{
			return this.executor.execute(callback);
		}catch(Throwable e){
			if(hasThrows){
				throw new HttpExecutorException(e);
			}
			if(logger.isInfoEnabled()){
				logger.info("http executor error",e);
			}
		}
		return null;
	}

	public boolean isHasThrows() {
		return hasThrows;
	}

	public void setHasThrows(boolean hasThrows) {
		this.hasThrows = hasThrows;
	}
	
	
}
